package cn.powertime.evaluation.app.controller;

import cn.powertime.evaluation.entity.SysFile;
import cn.powertime.evaluation.exception.FileServerException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

/**
 * @author zh
 * @version V1.0
 * @Package cn.powertime.evaluation.app.controller
 * @date 2019/12/23 15:58
 */
@RestController
public class FileAppController {


    @GetMapping("/app")
    @ApiOperation(value = "文件下载", notes = "下载上传的文件")
    public void downLoadFile( HttpServletResponse response) {
        final String fileName = "__UNI__A06CAEF_1223161354.apk";
        final String filePath = "D:\\\\app\\20191111";

        String downloadPath = filePath + File.separator + fileName;
        String origName = "__UNI__A06CAEF_1223161354.apk";
        try {
            // 设置response参数，可以打开下载页面
            response.reset();
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            String filename = URLEncoder.encode(origName, "UTF-8");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
            File downloadFile = new File(downloadPath);
            if (!downloadFile.exists()) {
                //throw new FileServerException(downloadPath + "文件不存在！");
            }
            Files.copy(downloadFile.toPath(), response.getOutputStream());
        } catch (IOException e) {
            //throw new FileServerException("下载文件异常！", e);
        }
    }


}
